package org.meal.service.impl;

import cn.meal.entity.Menu;
import cn.meal.mapper.MenuMapper;
import cn.meal.vo.ParentMenuVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.meal.repository.MenuRepository;
import org.meal.service.MenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private MenuRepository menuRepository;

    /**
     * 如果是父级菜单，子菜单将一起删除
     * @param menuId
     */
    @Override
    @Transactional()
    public void deleteMenuByMenuId(Integer menuId) {
        // 1.查看这条菜单是否是一级菜单
        Menu menu = baseMapper.selectById(menuId);
        if(menu.getParentId()==0){
            // 2.如果是父级菜单，将子菜单一起删除
            QueryWrapper<Menu> wrapper = new QueryWrapper<>();
            wrapper.eq("parent_id",menu.getId());
            baseMapper.delete(wrapper);

        }

//        menu.setUpdateTime(new Date());
//        baseMapper.updateById(menu);
        // 逻辑删除
        baseMapper.deleteById(menuId);

        menuRepository.deleteAll();

        this.savaMenuToMongoDB();
    }

    /**
     * 根据饭店ID,得到这个饭店下的菜单
     * @param storeId
     * @return
     */
    @Override
    public List<ParentMenuVo> getParentMenuByStoreId(Integer storeId) {

        // 1.先去mongoDB中查找
        List<Menu> menus=menuRepository.getMenuByIdAndParentId(storeId,0);
        if(menus.isEmpty()){

            // 2.去数据库查找存入缓存
            this.savaMenuToMongoDB();
            menus=menuRepository.getMenuByIdAndParentId(storeId,0);

            // 3.如果数据库中有数据，就将数据库写入缓存
            if(menus.isEmpty()){
                return null;
            }
        }

        // 4.将返回的结果进行包装处理
        List<ParentMenuVo> results=new ArrayList<>();
        ParentMenuVo parentMenuVo=null;
        for(Menu menu : menus){
            parentMenuVo = new ParentMenuVo();
            BeanUtils.copyProperties(menu,parentMenuVo);
            results.add(parentMenuVo);
        }
        return results;
    }

    @Override
    public void saveMenu(Menu menu) {

        //添加一条菜单
        baseMapper.insert(menu);

        //加入缓存
        if(menu.getParentId() == 0){
            menuRepository.save(menu);
        }else{
            Menu parentMenu = menuRepository.getMenuById(menu.getParentId());
            parentMenu.getChildMenu().add(menu);

            menuRepository.save(parentMenu);
        }


    }

    @Override
    public Menu getMenus(Integer storeId, Integer parentId) {

        // 1.去缓存中查找
        Menu menu=menuRepository.getMenuById(parentId);

        // 2.如果缓存中没有,去数据库中找
        if(menu==null){
            menuRepository.deleteAll();
            this.savaMenuToMongoDB();
            menu=menuRepository.getMenuById(parentId);
        }
        return menu;
    }

    @Override
    public void updateMenu(Menu menu) {
        baseMapper.updateById(menu);
        menuRepository.deleteAll();
        this.savaMenuToMongoDB();
    }

    /**
     * 将菜单保存存入缓存
     */
    private void savaMenuToMongoDB(){
        QueryWrapper<Menu> wrapper=new QueryWrapper<>();
        wrapper.eq("parent_id",0);
        List<Menu> menus=baseMapper.selectList(wrapper);
        //
        for(Menu menu : menus){
            wrapper.clear();
            wrapper.eq("parent_id",menu.getId());
            menu.setChildMenu(baseMapper.selectList(wrapper));
        }

        menuRepository.insert(menus);
    }

}
